<?php

if (! function_exists('b_d3downloads_download_show') ) {
	function b_d3downloads_download_show( $options )
	{
		$db =& Database::getInstance() ;
		$myts =& MyTextSanitizer::getInstance() ;

		require_once dirname( dirname(__FILE__) ).'/include/common_functions.php' ;
		require_once dirname(dirname(__FILE__)).'/include/upload_functions.php' ;

		$mydirname = empty( $options[0] ) ? 'd3downloads' : $options[0] ;
		$download_id = $options[1] ;
		$this_template = empty( $options[2] ) ? 'db:'.$mydirname.'_block_download.html' : trim( $options[2] ) ;

		if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

		$mod_url = XOOPS_URL.'/modules/'.$mydirname ;
		$module_handler =& xoops_gethandler('module');
		$config_handler =& xoops_gethandler('config');
		$module =& $module_handler->getByDirname( $mydirname );
		$mod_config =& $config_handler->getConfigsByCat(0, $module->getVar('mid'));

		$whr_cat4read = "d.cid IN (".implode(",",d3download_can_read( $mydirname ) ).")" ;
		$result = $db->query("SELECT d.lid, d.cid, d.title, d.url, d.filename, d.ext, d.homepage, d.version, d.size, d.platform, d.logourl, d.description, d.html, d.smiley, d.br, d.xcode, d.submitter, d.date, d.hits, d.rating, d.votes, d.comments, c.title FROM ".$db->prefix( $mydirname."_downloads" )." d LEFT JOIN ".$db->prefix($mydirname."_cat")." c ON d.cid=c.cid WHERE d.lid='".$download_id."' AND d.visible = '1' AND ($whr_cat4read)");

		$download4assign = array();
		$broken_link = 0 ;
		while( list( $id, $cid, $title, $url, $fname, $ex, $homepage, $version, $size, $platform, $logourl, $description, $html, $smiley, $br, $xcode, $submitter, $date, $hits, $rating, $votes, $comments, $category ) = $db->fetchRow( $result ) ) {
			$id4assign = intval( $id )  ;
			$cid4assign = intval( $cid ) ;
			$url4assign = $myts->makeTboxData4Show( $url ) ;
			$filename = $myts->makeTboxData4Show( $fname ) ;
			$ext = $myts->makeTboxData4Show( $ex ) ;
			if ( ! preg_match("/^http:\/\//", $url4assign ) ) {
				if( ! file_exists( $url4assign ) ){
					$broken_link = 1 ;
					$gif_image = "";
				} else {
					$filelink =  '<a href="'.$mod_url.'/index.php?page=visit&cid='.$cid4assign.'&lid='.$id4assign.'">' ;
					if( empty( $filename ) ){
						$f_info = pathinfo( $url4assign );
						$filename = $f_info['basename'];
					}
					$gif_image = d3download_return_gif_image( $ext );
				}
			} else {
				$filelink =  '<a href="'.$mod_url.'/index.php?page=visit&cid='.$cid4assign.'&lid='.$id.'" rel="nofollow">' ;
				$gif_image = 'download.gif';
			}
			$logourl4assign = ! empty( $logourl ) ? $myts->makeTboxData4Show( $logourl ) :"";
			$shots_link = d3download_shots_link( $mydirname, $cid4assign, $url, $filelink, $logourl4assign ) ;
			$submitter =  intval( $submitter ) ;
			$postname = d3download_postname( $submitter );
			$body = $myts->displayTarea( $description, intval( $html ), intval( $smiley ), intval( $xcode ), 1, intval( $br ) ) ;
			if ( strstr ( $body , '[pagebreak]' ) ){
				$desc_string = explode( '[pagebreak]', $body , 2 ) ;
				$body4assign = $desc_string[0].'<br /><div align="right"><a href="'.XOOPS_URL.'/modules/'.$mydirname.'/index.php?page=singlefile&amp;cid='.$cid4assign.'&lid='.$id4assign.'">'._MD_D3DOWNLOADS_LANG_SHOWSINGLEFILE.'</a></div>';
			} else {
				$body4assign = $body ;
			}
			$new = d3download_newdownloadgraphic( $date, $mydirname, $id4assign) ;
			$pop = d3download_popgraphic( $hits , $mydirname );
			$title4assign = $myts->makeTboxData4Show( $title ) ;
			$download4assign = array(
				'id' => $id4assign ,
				'cid' => $cid4assign ,
				'category' => $myts->makeTboxData4Show( $category ) ,
				'title' => $title4assign ,
				'url' => $url4assign ,
				'filelink' =>  $filelink ,
				'broken_link' =>  $broken_link ,
				'gif_image' =>  $gif_image ,
				'homepage' => $myts->makeTboxData4Show( $homepage ) ,
				'version' => $myts->makeTboxData4Show( $version ) ,
				'size' =>  b_d3downloads_PrettySize( intval( $size ) ),
				'platform' => $myts->makeTboxData4Show( $platform ) ,
				'description' => $body4assign ,
				'postname' =>  $postname,
				'updated' => formatTimestamp(intval( $date ),'s') ,
				'date' => intval( $date ) ,
				'hits' => intval( $hits ) ,
				'rating' => intval( $rating ) ,
				'votes' => intval( $votes ) ,
				'comments' => intval( $comments ) ,
				'new' => $new ,
				'pop' => $pop ,
				'shots' => $shots_link ,
			) ;
		}

		$canuseshots = ! empty( $mod_config['useshots'] ) ? 1 : 0 ;

		if( ! empty( $download4assign ) ){
			$block['download'] = $download4assign ;
			$block['mydirname'] = $mydirname ;
			$block['mod_url'] = $mod_url ;
			$block['canuseshots'] = $canuseshots ;
			$block['lang_title'] = _MB_D3DOWNLOADS_LANG_TITLE;
			$block['lang_ctitle'] = _MB_D3DOWNLOADS_LANG_CTITLE;
			$block['lang_version'] = _MD_D3DOWNLOADS_LANG_VERSION;
			$block['lang_updated'] = _MB_D3DOWNLOADS_LANG_DATE;
			$block['lang_description'] = _MD_D3DOWNLOADS_LANG_DESCRIPTION;
			$block['lang_hits'] = _MB_D3DOWNLOADS_LANG_HITS;
			$block['lang_size'] = _MD_D3DOWNLOADS_LANG_SIZE;
			$block['lang_kb'] = _MD_D3DOWNLOADS_LANG_KB;
			$block['lang_platform'] = _MD_D3DOWNLOADS_LANG_PLATFORM;
			$block['lang_homepage'] = _MD_D3DOWNLOADS_LANG_HOMEPAGE;
			if( empty( $options['disable_renderer'] ) ) {
				require_once XOOPS_ROOT_PATH.'/class/template.php' ;
				$tpl =& new XoopsTpl() ;
				$tpl->assign( 'block' , $block ) ;
				$ret['content'] = $tpl->fetch( $this_template ) ;
				return $ret ;
			} else {
				return $block ;
			}
		}
	}
}

if (! function_exists('b_d3downloads_download_edit') ) {
	function b_d3downloads_download_edit( $options )
	{
		$db =& Database::getInstance() ;
		$myts =& MyTextSanitizer::getInstance() ;
		$mydirname = empty( $options[0] ) ? 'd3downloads' : $options[0] ;
		$download_id = intval( @$options[1] ) ;
		$this_template = empty( $options[2] ) ? 'db:'.$mydirname.'_block_download.html' : trim( $options[2] ) ;

		if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

		// get downloads_title
		$downloaddata = array( 0 => '--' ) ;
		$result = $db->query( "SELECT lid, title FROM ".$db->prefix( $mydirname."_downloads" )." ORDER BY lid" ) ;
		while( list( $id , $title ) = $db->fetchRow( $result ) ) {
			$downloaddata[ $id ] = sprintf('%06d',$id).': '.$myts->makeTboxData4Show( $title ) ;
		}

		require_once XOOPS_ROOT_PATH.'/class/template.php' ;
		$tpl =& new XoopsTpl() ;
		$tpl->assign( array(
			'mydirname' => $mydirname ,
			'download' => $downloaddata ,
			'download_id' => $download_id ,
			'this_template' => $this_template ,
		) ) ;
		return $tpl->fetch( 'db:'.$mydirname.'_blockedit_download.html' ) ;
	}
}
if (! function_exists('b_d3downloads_PrettySize') ) {
	function b_d3downloads_PrettySize( $size )
	{
		$mb = 1024*1024;
		if ( $size > $mb ) {
			$mysize = sprintf ("%01.2f",$size/$mb) . " MB";
		} elseif ( $size >= 1024 ) {
			$mysize = sprintf ("%01.2f",$size/1024) . " KB";
		} else {
		    $mysize = sprintf( _MB_D3DOWNLOADS_NUMBYTES , $size);
		}
		return $mysize;
	}
}

?>

